package com.woniu.view;

import com.woniu.controller.*;
import com.woniu.entity.*;

import java.text.SimpleDateFormat;
import java.util.*;

public class ToIntendantPage {
    public static void intendantPage(Users users) {
        boolean flag = true;
        while (flag){
            System.out.println("_________________________");
            System.out.println("|------欢迎进入用户系统-----|");
            System.out.println("|--------1.用户管理--------|");
            System.out.println("|--------2.商家管理-----|");
            System.out.println("|--------3.商品管理--------|");
            System.out.println("|--------4.订单管理-----------|");
            System.out.println("|--------5.数据统计-----------|");
            System.out.println("|--------6.个人信息-----------|");
            System.out.println("|--------7.退出-----------|");
            System.out.println("--------------------------");
            Scanner sc = new Scanner(System.in);
            System.out.println("请选择你的操作：");
            int choice=sc.nextInt();
            switch (choice){
                case 1:
                    System.out.println("用户管理");
                    manageUsers();
                    break;
                case 2:
                    System.out.println("商家管理");
                    managemerchant();
                    break;
                case 3:
                    System.out.println("商品管理");
                    manageProduct();
                    break;
                    case 4:
                    System.out.println("订单管理");
                    manageOrders();
                    break;
                    case 5:
                    System.out.println("数据统计");
                        dataStatistics();
                    break;
                    case 6:
                    System.out.println("个人信息");
                        viewprofile(users);
                    break;
                case 7:
                    flag = false;//结束循环(退出系统)
                    break;
                default:
                    System.out.println("你输入有误！请重选选择");
                    break;
            }
        }
    }
    /**
     * 查看个人信息
     * */
    public static void viewprofile(Users users) {
        Power power = PowerController.selectPowerById(users.getPower_id());
        System.out.println("id\t姓名\t密码\t电话\t余额\t身份");
        System.out.println(users.getUser_id()+"\t"+users.getUsername()+"\t"+users.getPwd()+"\t"+users.getTelphone()+"\t"+users.getMoney()+"\t"+power.getPname());
        System.out.println("是否更改个人信息");
        System.out.println("输入y更改");
        System.out.println("输入n不更改");
        Scanner sc = new Scanner(System.in);
        String ch=sc.nextLine();
        if(ch.equals("y")){
            System.out.println("选择要修改的信息");
            System.out.println("1.修改用户名");
            System.out.println("2.修改密码");
            System.out.println("3.修改手机号");
            int choice=sc.nextInt();
            switch (choice){
                case 1:
                    System.out.println("输入修改后的名字");
                    String name = sc.next();
                    UserController.updateUserName(name, users.getUser_id());
                    System.out.println("修改成功");
                    break;
                case 2:
                    System.out.println("输入要修改的密码");
                    String password = sc.next();
                    UserController.updateUserPwd(password,users.getUser_id());
                    System.out.println("修改成功");
                    break;
                case 3:
                    System.out.println("修改要修改的手机号");
                    String phone = sc.next();
                    UserController.updateUserTelphone(phone,users.getUser_id());
                    System.out.println("修改成功");
                    break;
                default:
                    System.out.println("无此操作");
                    break;
            }
        }else if(ch.equals("n")){
            System.out.println("退出");
        }
    }
    /**
     * 数据统计
     * */
    public static void dataStatistics() {
        List<Orders> orders = OrdersController.selectOrdersBStatus(5);
        int count=0;
        double sum=0;
        for (Orders order : orders) {
            sum+=order.getTotal();
            List<OrderItems> orderItems = OrderItemsCotroller.getOrderItems(order.getOrder_id());
            for (OrderItems orderItem : orderItems) {
                count++;
            }
        }
        System.out.println("交易数量为:"+count);
        System.out.println("交易额为:"+sum);
    }
    /**
     * 订单管理
     * */
    public static void manageOrders(){
        System.out.println("请选择操作");
        Scanner sc = new Scanner(System.in);
        System.out.println("1.查看订单");
        System.out.println("2.处理纠纷");
        int choice=sc.nextInt();
        switch (choice){
            case 1:
                System.out.println("查看订单");
                chackOrders();
                break;
                case 2:
                    System.out.println("处理纠纷");
                    solveDispute();
                    break;
                    default:
                        System.out.println("无此操作");
                        break;
        }
    }
    /**
     * 处理纠纷
     * */
    public static void solveDispute(){
        System.out.println("纠纷订单:");
        List<OrderItems> orderItemsByStatus = OrderItemsCotroller.getOrderItemsByStatus(6);
        for (OrderItems orderItem : orderItemsByStatus) {
            System.out.println(orderItem);
        }
        System.out.println("联系商家...");
        System.out.println("联系买家...");
        System.out.println("处理结果:");
        System.out.println("1.退款");
        System.out.println("2.不退款");
        Scanner sc = new Scanner(System.in);
        int choice=sc.nextInt();
        switch (choice){
            case 1:
                System.out.println("退款");
                refund();
                break;
                case 2:
                    System.out.println("不退款");
                    noRefund();
                    break;
                    default:
                        System.out.println("无此操作");
                        break;
        }
    }
    /**
     * 退款
     * */
    public static void refund(){
        List<OrderItems> orderItemsByStatus = OrderItemsCotroller.getOrderItemsByStatus(6);
        Set<Integer> set=new HashSet<>();
        for (OrderItems orderItem : orderItemsByStatus) {
            set.add(orderItem.getOrder_id());
            ProductController.updateProductStock(orderItem.getProduct_id(),-orderItem.getQuantity());
            UserController.updateUserMoney(orderItem.getMerchant_id(),orderItem.getPrice());
            Orders orders = OrdersController.selectOrdersByOrderID(orderItem.getOrder_id());
            UserController.updateUserMoney(orders.getUser_id(),-orderItem.getPrice());
            OrdersController.updateOrderstotal(orderItem.getOrder_id(),orderItem.getPrice());
            OrderItemsCotroller.deleteOrderItems(orderItem.getOrder_id());
        }
        for (Integer integer : set) {
            List<OrderItems> orderItems = OrderItemsCotroller.getOrderItems(integer);
            if (orderItems.size() == 0) {
                OrdersController.deleteOrdersByorderId(integer);
            }
        }
        System.out.println("纠纷解决，退款成功");
    }
    /**
     * 不退款
     * */
    public static void noRefund(){
        List<OrderItems> orderItemsByStatus = OrderItemsCotroller.getOrderItemsByStatus(6);
        for (OrderItems orderItem : orderItemsByStatus) {
            OrderItemsCotroller.updateOrderItemsByOrderItemId(orderItem.getOrder_item_id(),1);
            OrdersController.updateOrdersStatus(orderItem.getOrder_id(),1);
        }
        System.out.println("纠纷解决，不退款");
    }

    /**
     * 查看订单及详情
     * */
    public static void chackOrders(){
        System.out.println("请选择查看模式");
        System.out.println("1.查看所有订单");
        System.out.println("2.通过商品和商家查");
        Scanner sc = new Scanner(System.in);
        int choice=sc.nextInt();
        if (choice == 1) {
            List<Orders> allOrders = OrdersController.getAllOrders();
            for (Orders order : allOrders){
                System.out.println("订单编号\t订单价格\t订单状态\t订单时间");
                Date updatedAt = order.getUpdated_at();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = formatter.format(updatedAt);
                State state = StateController.selectStateById(order.getStatus());
                System.out.println(order.getOrder_id()+"\t"+order.getTotal()+"\t"+state.getStatus()+"\t"+format);
                List<OrderItems> orderItems = OrderItemsCotroller.getOrderItems(order.getOrder_id());
                System.out.println("订单详情:");
                System.out.println("订单详情编号\t商品\t数量\t价格\t商品详情\t订单状态\t商家");
                for (OrderItems orderItem : orderItems){
                    Product productById = ProductController.getProductById(orderItem.getProduct_id());
                    State state1 = StateController.selectStateById(orderItem.getStatus());
                    Integer merchantId = productById.getMerchant_id();
                    Users users = UserController.selectUserByUid(merchantId);
                    System.out.println(orderItem.getOrder_item_id()+"\t"+productById.getPname()+"\t"+orderItem.getQuantity()+"\t"+orderItem.getPrice()+"\t"+productById.getDes()+"\t"+state1.getStatus()+"\t"+ users.getUsername());
                }
            }
        }else if (choice == 2) {
            System.out.println("请输入商品的名字");
            String pname = sc.next();
            List<Product> products = ProductController.selectProductByName(pname);
            System.out.println("请输入商家的名字");
            String mname = sc.next();
            Users users = UserController.selectUserByName(mname);
            System.out.println("订单编号\t商品\t数量\t价格\t商品详情\t订单状态\t商家");
            if (pname.equals("null")) {
               List<OrderItems> orderItemsByMid = OrderItemsCotroller.getOrderItemsByMid(users.getUser_id());
               for (OrderItems orderItem : orderItemsByMid) {
                   Product product = ProductController.getProductById(orderItem.getProduct_id());
                   State state = StateController.selectStateById(orderItem.getStatus());
                   System.out.println(orderItem.getOrder_item_id()+"\t"+product.getPname()+"\t"+orderItem.getQuantity()+"\t"+orderItem.getPrice()+"\t"+product.getDes()+"\t"+state.getStatus()+"\t"+ users.getUsername());
               }
            }else if (mname.equals("null")) {
                for (Product product : products) {
                   List<OrderItems> orderItemsByPid = OrderItemsCotroller.getOrderItemsByPid(product.getMerchant_id(), product.getPid());
                   for (OrderItems orderItem : orderItemsByPid) {
                       State state = StateController.selectStateById(orderItem.getStatus());
                       System.out.println(orderItem.getOrder_item_id()+"\t"+product.getPname()+"\t"+orderItem.getQuantity()+"\t"+orderItem.getPrice()+"\t"+product.getDes()+"\t"+state.getStatus()+"\t"+ orderItem.getMerchant_id());
                   }
                }
            }else {
                for (Product product : products){
                    List<OrderItems> orderItemsIf = OrderItemsCotroller.getOrderItemsIf(product.getPid(), users.getUser_id());
                    for (OrderItems orderItem : orderItemsIf){
                        State state = StateController.selectStateById(orderItem.getStatus());
                        System.out.println(orderItem.getOrder_item_id()+"\t"+product.getPname()+"\t"+orderItem.getQuantity()+"\t"+orderItem.getPrice()+"\t"+product.getDes()+"\t"+state.getStatus()+"\t"+ users.getUsername());
                    }
                }
            }
        }
    }
    /**
     * 商品管理
     * */
    public static void manageProduct(){
        System.out.println("请选择操作");
        System.out.println("1.商品审核");
        System.out.println("2.下架商品");
        Scanner sc = new Scanner(System.in);
        int choice=sc.nextInt();
        switch (choice){
            case 1:
                System.out.println("商品审核");
                checkProduct();
                break;
                case 2:
                    System.out.println("下架商品");
                    soldout();
                    break;
                    default:
                        System.out.println("无此操作");
                        break;
        }
    }
    /**
     * 下架商品
     * */
    public static void soldout(){
       List<Product> productList = ProductController.getProductList();
       for (Product product : productList){
           System.out.println(product);
       }
       System.out.println("请选择你要下架的商品id");
       Scanner sc = new Scanner(System.in);
       int pid=sc.nextInt();
       ProductController.deleteProduct(pid);
       System.out.println("下架成功");
    }
/**
 * 商品审核
 * */
public static void checkProduct(){
    System.out.println("需审核商品");
    List<WaitPutaway> waitPutaways = WaitPutawayController.selectWaitPutaway();
    for (WaitPutaway waitPutaway : waitPutaways){
        System.out.println(waitPutaway);
    }
    Scanner sc = new Scanner(System.in);
    System.out.println("是否上架或退返");
    System.out.println("输入1上架");
    System.out.println("输入2退返");
    System.out.println("输入其他退出");
    int choice=sc.nextInt();
    if(choice==1){
        System.out.println("输入要上架的商品id");
        int wid=sc.nextInt();
        WaitPutaway waitPutaway = WaitPutawayController.selectWaitPutawayByWid(wid);
        Product product = new Product(null,waitPutaway.getPname(),waitPutaway.getPrice(),waitPutaway.getStock(),waitPutaway.getDes(),new Date(),new Date(),waitPutaway.getMerchant_id());
        ProductController.insertProduct(product);
        WaitPutawayController.deleteWaitPutaway(waitPutaway.getWid());
        System.out.println("上架成功");
    }else if(choice==2){
        System.out.println("输入要退返的商品id");
        int wid=sc.nextInt();
        WaitPutawayController.deleteWaitPutaway(wid);
        System.out.println("已退返");
    }else {
        System.out.println("退出审核");
    }

}
    /**
     * 商家管理
     * */
    private static void managemerchant() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择操作");
        System.out.println("1.商家入驻");
        System.out.println("2.商家注销");
        Integer choice=sc.nextInt();
        switch (choice){
            case 1:
                System.out.println("商家入驻");
                merchantRegister();
                break;
            case 2:
                System.out.println("商家注销");
                merchantLogout();
                break;
                default:
                    System.out.println("无此操作");
                    break;
        }
    }
    /**
     * 商家注销
     * */
    public static void merchantLogout() {
        for (Users users : UserController.selectAllUsersByPowerId(2)) {
            System.out.println(users);
        }
        System.out.println("请输入要注销商家的id");
        Scanner sc = new Scanner(System.in);
        Integer uid=sc.nextInt();
        boolean flag = true;
        List<OrderItems> orderItemsByMid = OrderItemsCotroller.getOrderItemsByMid(uid);
        for (OrderItems orderItems : orderItemsByMid) {
            Integer status = orderItems.getStatus();
            if (status == 1 || status == 3 || status == 4 || status ==6) {
                flag = false;
                break;
            }
        }
        if (flag) {
            OrderItemsCotroller.deleteByMid(uid,2);
            OrderItemsCotroller.deleteByMid(uid,5);
            ProductController.deleteProductByMerchantId(uid);
            GoodsController.deleteGoodsByMerchantId(uid);
            UserController.deleteUserById(uid);
            System.out.println("注销成功");
        }else {
            System.out.println("商家还有订单未完成，不能注销");
        }
    }
    /**
     * 商家入驻
     * */
    public static void merchantRegister() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入商家名");
        String username=sc.next();
        System.out.println("请输入密码");
        String password=sc.next();
        System.out.println("请输入手机号");
        String telephone=sc.next();
        Users users1 = UserController.selectUserByName(username);
        if(users1!=null){
            System.out.println("商家已存在");
        }else {
            Users users = new Users(null,username,password,telephone,new Date(),new Date(),2,0.0);
            UserController.insertUser(users);
            System.out.println("入住成功");
        }
    }
    /**
     * 用户管理
     * */
public static void manageUsers() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择操作");
        System.out.println("1.用户注册");
        System.out.println("2.用户注销");
        Integer choice=sc.nextInt();
        switch (choice){
            case 1:
                System.out.println("用户注册");
                userRegister();
                break;
                    case 2:
                        System.out.println("用户注销");
                        userLogout();
                        break;
                        default:
                            System.out.println("无此操作");
                            break;
        }
}

/**
 * 注册
 * */
public static void userRegister(){
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入用户名");
    String username=sc.next();
    System.out.println("请输入密码");
    String password=sc.next();
    System.out.println("请输入手机号");
    String telephone=sc.next();
    Users users1 = UserController.selectUserByName(username);
    if(users1!=null){
        System.out.println("用户已存在");
    }else {
        Users users = new Users(null,username,password,telephone,new Date(),new Date(),3,0.0);
        UserController.insertUser(users);
        System.out.println("注册成功");
    }
}

/**
 * 注销
 * */
public static void userLogout() {
    for (Users users : UserController.selectAllUsersByPowerId(3)) {
        System.out.println(users);
    }
    System.out.println("请输入要注销用户的id");
    Scanner sc = new Scanner(System.in);
    Integer uid =sc.nextInt();
    boolean flag = true;
    List<Orders> allOrders1 = OrdersController.getAllOrders(uid);
    for (Orders orders : allOrders1) {
        if (orders.getStatus()!=2 && orders.getStatus()!=5){
            flag = false;
            break;
        }
    }
    if (flag){
        List<Orders> allOrders = OrdersController.getAllOrders(uid);
        for (Orders orders : allOrders) {
            OrderItemsCotroller.deleteOrderItems(orders.getOrder_id());
        }
        OrdersController.deleteOrder(uid);
        GoodsController.deleteGoodsByUid(uid);
        UserController.deleteUserById(uid);
        System.out.println("注销成功");
    }else {
        System.out.println("该还有未完成的订单,无法注销");
    }
}


}
